package com.bykj.taketurns.findfragment.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bykj.taketurns.R;
import com.bykj.taketurns.findfragment.bean.FindBean;

import java.util.List;


class FindItemAdapter extends RecyclerView.Adapter<FindItemAdapter.ViewHolder> implements View.OnClickListener {

    private Context context;
    private List<FindBean.TypeListBean.StoryVideoSetBean> list;


    public FindItemAdapter(Context context, List<FindBean.TypeListBean.StoryVideoSetBean> storyVideoSet) {
                this.context = context;
                this.list = storyVideoSet;
    }
    private OnItemClickListener mOnItemClickListener = null;


    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(view, (Integer) view.getTag());
        }
    }

    void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.item_script_rv_item, parent, false);

        ViewHolder viewHolder = new ViewHolder(view);

        view.setOnClickListener(this);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        holder.itemView.setTag(position);
        holder.tv_name_script_item.setText(list.get(position).getUnickname());
        holder.tv_sum_script_item.setText(list.get(position).getSvbrowse() + "");
        Glide.with(context).load(list.get(position).getSvimg()).placeholder(R.mipmap.aa).into(holder.iv_script_item);

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        ImageView iv_script_item;
        TextView tv_name_script_item;
        TextView tv_sum_script_item;

        public ViewHolder(View view) {
            super(view);

            iv_script_item = view.findViewById(R.id.iv_script_item);
            tv_name_script_item = view.findViewById(R.id.tv_name_script_item);
            tv_sum_script_item = view.findViewById(R.id.tv_sum_script_item);

        }
    }
}
